package tsou.cn.glidetest;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.FutureTarget;
import com.bumptech.glide.request.Request;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SizeReadyCallback;
import com.bumptech.glide.request.target.Target;

import java.io.File;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import tsou.cn.glidetest.Util.UIUtils;

public class CrossFadeActivity extends AppCompatActivity {

    private ImageView mIv;
    private TextView mPath;
    String url = "https://unsplash.it/200/210?random&36";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_url_have_token);
        initView();
        downLoad();
    }

    private void initView() {
        mIv = (ImageView) findViewById(R.id.iv);
        mPath = (TextView) findViewById(R.id.path);

//        Glide.with(this)
//                .load(url)
//                .crossFade(5000)
//                .into(mIv);

    }


    private void downLoad() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                FutureTarget<File> fileFutureTarget = Glide.with(UIUtils.getContext())
                        .load(url)
                        .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
                try {
                    final File file = fileFutureTarget.get();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mPath.setText(file.getPath());
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(new Runnable() {

            private Bitmap bitmap;

            @Override
            public void run() {
                try {
                    bitmap = Glide.with(UIUtils.getContext())
                            .load(url)
                            .asBitmap()
                            .into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                            .get();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mIv.setImageBitmap(bitmap);
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }


}
